<template>
  <!-- 外部项目申报 流程 -->
  <div class="outside_index">
    <div class="left">
      <Top class="top_div" @topClickFn="topClickFn" />

      <div class="bottom">
        <component
          :is="comName"
          :isStart="isStart"
          :form="form"
          :peopleList="peopleList"
          :supplement="supplementData"
          :isDisadled="isDisadled"
          @saveProjectFn="saveProjectFn"
          @addPeopleLine="addPeopleLine"
          @delPeopleLine="delPeopleLine"
        />
      </div>
    </div>
    <div class="right">
      <Steps />
    </div>
  </div>
</template>

<script>
import moment from "moment";

import {
  apiProjectInfo, // 获取申报详情
  apiSubmitProject, // 申报提交
  apitempSave, // 暂存申报
  apiPeoList, // 10 获取人员列表
  apiExportProjectWord, // 12 导出文档
  apiProjectProcess, // 17 查询项目流程
  apiUpdateSubmitProject, // 14 更新后提交项目
  apiUpdateTempSave, // 15 更新后暂存
} from "@/api2/projectManagement/reporting";

import { getToken } from "@/utils/auth";

import Steps from "../components/steps.vue";
import Top from "../components/top.vue";

import Approval from "./coms/approval.vue"; // 项目立项
import Supplement from "./coms/supplement.vue"; // 补充材料

export default {
  components: {
    Steps,
    Top,
    Approval,
    Supplement,
  },
  computed: {
    // 是否开始申报. 没有开始申报，只能暂存
    isStart() {
      return this.$store.state.project.projectInfo.isStart;
    },
    // 项目类型
    pType() {
      return this.$store.state.project.projectInfo.type;
    },

    isCreateNew() {
      return this.$store.state.project.projectInfo.isCreateNew;
    },
  },
  data() {
    return {
      projectId: null,
      isDisadled: false, // 审批的时候，不能填写
      step_1: require("@/assets/static/imgs/step_1.png"),
      step_2: require("@/assets/static/imgs/step_2.png"),
      step_3: require("@/assets/static/imgs/step_3.png"),
      step_4: require("@/assets/static/imgs/step_4.png"),
      step_5: require("@/assets/static/imgs/step_5.png"),
      peopleList: [], // 选择的人员列表
      supplementData: {}, // 补充材料
      comName: "Approval",
      headers: {
        Authorization: getToken(),
        platform: "WEB",
      },

      jingfei: [
        "咨询费",
        "差旅费",
        "培训/会议费",
        "劳务费",
        "专用设备购置费",
        "专用材料购置费",
        "图书文献数据费",
        "出版/文献/信息传播/知识产权事务费",
        "其他支出",
        "咨询费",
      ],

      form: {
        projectName: "", // 项目名称
        projectType: "", // 项目申报类别
        projectUnit: "", // 申报单位
        // specialistInfo: "", // 专家信息
        specialistInfo1: "", // 专家信息1 - 姓名
        specialistInfo2: "", // 专家信息2 - 职称
        // contractAddress: "", // 通讯地址及邮编
        contractAddress1: "", // 通讯地址1 - 地址
        contractAddress2: "", // 通讯地址2 - 邮编
        // contractPhone: "", // 手机号码及电子邮箱
        contractPhone1: "", // 1 手机号码
        contractPhone2: "", // 2 电子邮箱
        startTime: "", // 项目开始时间
        endTime: "", // 项目结束时间
        cooperateUnit: "", // 合作单位
        purpose: "", // 目的和意义
        base: "", // 实施基础与条件
        content: "", // 实施内容
        plan: "", // 实施计划进度
        target: "", //主要技术指标及预期
        // 考核指标分解
        // targets: [
        //   {
        //     index: 0,
        //     sort: 0,
        //     unitName: "",
        //     name: "",
        //     yusuan: 0,
        //     zichou: 0,
        //   },
        // ],
        // 实施人员
        user: [
          {
            index: 0,
            sort: 0,
            age: 0,
            userId: "",
            dept: "", //职务
            unit: "", //	单位
            work: "", // 担任工作
            name: "", //姓名
            // 注意：传的数据有所不同
            personAge: 0, //实施人员年龄
            personUnit: "", //	实施人员单位
            personWork: "", //实施人员承担的具体工作,
            personZhiji: "", //	实施人员职级
          },
        ],

        otherPerson: [
          {
            sort: "",
            age: 0,
            dept: "", //职务
            unit: "", //	单位
            work: "", // 担任工作
            name: "", //姓名
            // 注意：传的数据有所不同
            personAge: 0, //实施人员年龄
            personUnit: "", //	实施人员单位
            personWork: "", //实施人员承担的具体工作,
            personZhiji: "", //	实施人员职级
          },
        ],
        // 项目经费预算
        funds: [
          {
            index: 0,
            // name: "咨询费", // 	支出科目
            fundsEnumName: "咨询费", // 	支出科目
            name: "咨询费", // 	支出科目
            amount: 0, // 金额
            note: "", // 备注
            // 注：传参有所不同
            fundsAmount: 0, //	金额
            fundsEnumKey: 1, //	经费所属类型KEY
          },
          {
            index: 1,
            fundsEnumName: "差旅费", // 名称
            name: "差旅费", // 名称
            amount: 0, // 金额
            note: "", // 备注
            // 注：传参有所不同
            fundsAmount: 0, //	金额
            fundsEnumKey: 2, //	经费所属类型KEY
          },
          {
            index: 2,
            fundsEnumName: "培训/会议费", // 名称
            name: "培训/会议费", // 名称
            amount: 0, // 金额
            note: "", // 备注
            // 注：传参有所不同
            fundsAmount: 0, //	金额
            fundsEnumKey: 3, //	经费所属类型KEY
          },
          {
            index: 3,
            fundsEnumName: "劳务费", // 名称
            name: "劳务费", // 名称
            amount: 0, // 金额
            note: "", // 备注
            // 注：传参有所不同
            fundsAmount: 0, //	金额
            fundsEnumKey: 4, //	经费所属类型KEY
          },
          {
            index: 4,
            fundsEnumName: "专用设备购置费", // 名称
            name: "专用设备购置费", // 名称
            amount: 0, // 金额
            note: "", // 备注
            // 注：传参有所不同
            fundsAmount: 0, //	金额
            fundsEnumKey: 5, //	经费所属类型KEY
          },
          {
            index: 5,
            fundsEnumName: "专用材料购置费", // 名称
            name: "专用材料购置费", // 名称
            amount: 0, // 金额
            note: "", // 备注
            // 注：传参有所不同
            fundsAmount: 0, //	金额
            fundsEnumKey: 6, //	经费所属类型KEY
          },
          {
            index: 6,
            fundsEnumName: "图书文献数据费", // 名称
            name: "图书文献数据费", // 名称
            amount: 0, // 金额
            note: "", // 备注
            // 注：传参有所不同
            fundsAmount: 0, //	金额
            fundsEnumKey: 7, //	经费所属类型KEY
          },
          {
            index: 7,
            fundsEnumName: "出版/文献/信息传播/知识产权事务费", // 名称
            name: "出版/文献/信息传播/知识产权事务费", // 名称
            amount: 0, // 金额
            note: "", // 备注
            // 注：传参有所不同
            fundsAmount: 0, //	金额
            fundsEnumKey: 8, //	经费所属类型KEY
          },
          {
            index: 8,
            fundsEnumName: "其他支出", // 名称
            name: "其他支出", // 名称
            amount: 0, // 金额
            note: "", // 备注
            // 注：传参有所不同
            fundsAmount: 0, //	金额
            fundsEnumKey: 9, //	经费所属类型KEY
          },
        ],
        funds2: [],
        conditionFirst: "", // 保障条件1
        conditionSecond: "", //保障条件2
      },
      active: 0, //申报步骤
      stepsArr: [
        {
          index: 0,
          text: "申报",
          isChoose: false,
          isLast: false,
        },
        {
          index: 1,
          text: "审核",
          isChoose: false,
          isLast: false,
        },
        {
          index: 0,
          text: "登记结果",
          isChoose: false,
          isLast: true,
        },
      ],
      formRules: {
        projectName: [
          { required: true, message: "请输入项目名称", trigger: "blur" },
        ],
        primaryUnit: [
          { required: true, message: "请输入主管单位", trigger: "blur" },
        ],
        cooperateUnit: [
          { required: true, message: "请输入合作单位", trigger: "blur" },
        ],
        projectCycle: [
          {
            required: true,
            message: "请选择日期",
            trigger: "change",
          },
        ],
        appendixUrl: [
          {
            required: true,
            message: "请选择立项申报书",
            trigger: "change",
          },
        ],
      },
      tableData: [
        {
          index: 1,
          name: "",
          text: "",
          address: "",
          address1: "",
        },
      ],
    };
  },
  created() {
    if (this.$route.params.id) {
      this.projectId = this.$route.params.id;
      this.isDisadled = true;
      this.getDateil();
    }
    this.getPeoList();

    this.getProjectProcess(); // 项目审核流程
  },

  methods: {
    downloadFile(url, fileName) {
      const link = document.createElement("a");
      link.href = url;
      link.download = fileName;
      link.click();
    },

    // 获取项目审核流程
    getProjectProcess() {
      const params = this.projectId
        ? { projectId: this.projectId }
        : { projectCategory: this.pType };

      apiProjectProcess(params).then((res) => {
        const processArr = res.code == 0 ? res.data : [];
        if (processArr.length) {
          for (let i = 0; i < processArr.length; i++) {
            switch (i) {
              case 0:
                processArr[i].imgSrc = this.step_1;
                break;
              case 1:
                processArr[i].imgSrc = this.step_2;
                break;
              case 2:
                processArr[i].imgSrc = this.step_3;
                break;
              case 3:
                processArr[i].imgSrc = this.step_4;
                break;
              default:
                processArr[i].imgSrc = this.step_5;
            }
          }
        }
        this.$store.commit("project/SET_PROCESSLIST", processArr);
      });
    },

    getDateil() {
      apiProjectInfo(this.projectId).then((res) => {
        if (res.code === 0) {
          const info = res.data;
          const zhuanjia = info.specialistInfo?.split(",");
          const tongxun = info.contractAddress?.split(",");
          const shouji = info.contractPhone?.split(",");
          const content = JSON.parse(info.content);

          this.form = {
            projectName: info.projectName, // 项目名称
            projectType: info.projectType, // 项目申报类别
            projectUnit: info.projectUnit, // 申报单位
            specialistInfo1: zhuanjia[0], // 专家信息1 - 姓名
            specialistInfo2: zhuanjia[1], // 专家信息2 - 职称
            contractAddress1: tongxun[0], // 通讯地址1 - 地址
            contractAddress2: tongxun[1], // 通讯地址2 - 邮编
            contractPhone1: shouji[0], // 1 手机号码
            contractPhone2: shouji[1], // 2 电子邮箱
            startTime: info.startTime, // 项目开始时间
            endTime: info.endTime, // 项目结束时间
            cooperateUnit: info.cooperateUnit, // 合作单位
            purpose: content.purpose, // 目的和意义
            base: content.base, // 实施基础与条件
            content: content.content, // 实施内容
            plan: content.plan, // 实施计划进度
            target: content.target, //主要技术指标及预期
            // targets: content.targets, //考核指标分解
            conditionFirst: content.conditionFirst, // 保障条件1
            conditionSecond: content.conditionSecond, //保障条件2
          };
          // 人员 -- 内部
          let userArr = [];
          userArr = info.xmProjectPersonList;
          this.form.user = userArr.map((v) => {
            return {
              index: v.sort,
              sort: v.sort,
              id: v.sort,
              age: v.personAge,
              userId: v.userId,
              dept: v.personZhiji, //职务
              unit: v.personUnit, //	单位
              work: v.personWork, // 担任工作
              name: this.getUserName(v.userId), //姓名

              // 注意：传的数据有所不同
              personAge: 0, //实施人员年龄
              personUnit: "", //	实施人员单位
              personWork: "", //实施人员承担的具体工作,
              personZhiji: "", //	实施人员职级
            };
          });
          // 人员 -- 外部
          this.form.otherPerson = info.xmProjectOtherPersonList;

          // 经费 funds
          let fundsArr = info.xmProjectFundsList;

          let arr1 = [];
          let arr2 = [];

          for (let i = 0; i < fundsArr.length; i++) {
            if (fundsArr[i].projectPeriod == "REPORT") {
              arr1.push({
                index: i,
                fundsEnumName: this.jingfei[i], // 	支出科目
                name: this.jingfei[i], // 	支出科目
                amount: fundsArr[i].fundsAmount, // 金额
                note: fundsArr[i].note, // 备注
                fundsAmount: 0, //	金额
                fundsEnumKey: Number(i) + 1, //	经费所属类型KEY
              });
            } else {
              arr2.push({
                index: i,
                fundsEnumName: this.jingfei[i - 9], // 	支出科目
                name: this.jingfei[i - 9], // 	支出科目
                name: this.jingfei[i - 9], // 	支出科目
                amount: fundsArr[i].fundsAmount, // 金额
                note: fundsArr[i].note, // 备注
                fundsAmount: 0, //	金额
                fundsEnumKey: Number(i) + 1, //	经费所属类型KEY
              });
            }
          }
          this.form.funds = arr1;
          this.form.funds2 = arr2;
          // 保存点击的已审核完成的数据信息，用于导出Word
          sessionStorage.setItem("exportWordInfo", JSON.stringify(this.form));

          const { appendixUrl, xmProjectAppendix, xmProjectPeriodList } = info;
          // supplement
          this.supplementData = {
            appendixUrl,
            xmProjectAppendix,
            xmProjectPeriodList,
          };
        }
      });
    },

    // 取消
    resetForm() {
      this.form = {
        projectName: "", // 项目名称
        projectType: "", // 项目申报类别
        projectUnit: "", // 申报单位
        // specialistInfo: "", // 专家信息
        specialistInfo1: "", // 专家信息1 - 姓名
        specialistInfo2: "", // 专家信息2 - 职称
        // contractAddress: "", // 通讯地址及邮编
        contractAddress1: "", // 通讯地址1 - 地址
        contractAddress2: "", // 通讯地址2 - 邮编
        // contractPhone: "", // 手机号码及电子邮箱
        contractPhone1: "", // 1 手机号码
        contractPhone2: "", // 2 电子邮箱
        startTime: "", // 项目开始时间
        endTime: "", // 项目结束时间
        cooperateUnit: "", // 合作单位
        purpose: "", // 目的和意义
        base: "", // 实施基础与条件
        content: "", // 实施内容
        plan: "", // 实施计划进度
        target: "", //主要技术指标及预期
        // 考核指标分解
        // targets: [
        //   {
        //     index: 0,
        //     sort: 0,
        //     unitName: "",
        //     name: "",
        //     yusuan: 0,
        //     zichou: 0,
        //   },
        // ],
        // 实施人员
        user: [
          {
            index: 0,
            id: 0,
            sort: 0,
            age: 0,
            userId: "",
            dept: "", //职务
            unit: "", //	单位
            work: "", // 担任工作
            name: "", //姓名
            // 注意：传的数据有所不同
            personAge: 0, //实施人员年龄
            personUnit: "", //	实施人员单位
            personWork: "", //实施人员承担的具体工作,
            personZhiji: "", //	实施人员职级
          },
        ],
        otherPerson: [
          {
            sort: "",
            age: 0,
            dept: "", //职务
            unit: "", //	单位
            work: "", // 担任工作
            name: "", //姓名
            // 注意：传的数据有所不同
            personAge: 0, //实施人员年龄
            personUnit: "", //	实施人员单位
            personWork: "", //实施人员承担的具体工作,
            personZhiji: "", //	实施人员职级
          },
        ],
        // 项目经费预算
        funds: [
          {
            index: 0,
            fundsEnumName: "咨询费", // 	支出科目
            name: "咨询费", // 	支出科目
            fundsEnumKey: 1, //	经费所属类型KEY
            amount: 0, // 金额
            note: "", // 备注
            // 注：传参有所不同
            fundsAmount: 0, //	金额
          },
          {
            index: 1,
            fundsEnumName: "差旅费", // 名称
            name: "差旅费", // 名称
            fundsEnumKey: 2, //	经费所属类型KEY
            amount: 0, // 金额
            note: "", // 备注
            // 注：传参有所不同
            fundsAmount: 0, //	金额
          },
          {
            index: 2,
            fundsEnumName: "培训/会议费", // 名称
            name: "培训/会议费", // 名称
            fundsEnumKey: 3, //	经费所属类型KEY
            amount: 0, // 金额
            note: "", // 备注
            // 注：传参有所不同
            fundsAmount: 0, //	金额
          },
          {
            index: 3,
            fundsEnumName: "劳务费", // 名称
            name: "劳务费", // 名称
            fundsEnumKey: 4, //	经费所属类型KEY
            amount: 0, // 金额
            note: "", // 备注
            // 注：传参有所不同
            fundsAmount: 0, //	金额
          },
          {
            index: 4,
            fundsEnumName: "专用设备购置费", // 名称
            name: "专用设备购置费", // 名称
            fundsEnumKey: 5, //	经费所属类型KEY
            amount: 0, // 金额
            note: "", // 备注
            // 注：传参有所不同
            fundsAmount: 0, //	金额
          },
          {
            index: 5,
            fundsEnumName: "专用材料购置费", // 名称
            name: "专用材料购置费", // 名称
            fundsEnumKey: 6, //	经费所属类型KEY
            amount: 0, // 金额
            note: "", // 备注
            // 注：传参有所不同
            fundsAmount: 0, //	金额
          },
          {
            index: 6,
            fundsEnumName: "图书文献数据费", // 名称
            name: "图书文献数据费", // 名称
            fundsEnumKey: 7, //	经费所属类型KEY
            amount: 0, // 金额
            note: "", // 备注
            // 注：传参有所不同
            fundsAmount: 0, //	金额
          },
          {
            index: 7,
            fundsEnumName: "出版/文献/信息传播/知识产权事务费", // 名称
            name: "出版/文献/信息传播/知识产权事务费", // 名称
            fundsEnumKey: 8, //	经费所属类型KEY
            amount: 0, // 金额
            note: "", // 备注
            // 注：传参有所不同
            fundsAmount: 0, //	金额
          },
          {
            index: 8,
            fundsEnumName: "其他支出", // 名称
            name: "其他支出", // 名称
            fundsEnumKey: 9, //	经费所属类型KEY
            amount: 0, // 金额
            note: "", // 备注
            // 注：传参有所不同
            fundsAmount: 0, //	金额
          },
        ],
        funds2: [],
        conditionFirst: "", // 保障条件1
        conditionSecond: "", //保障条件2

        // appendixUrl: [], //上传的文件列表
      };
    },
    // 取消 0、暂存 1、新增 2  导出3
    saveProjectFn(i) {
      const content = {
        purpose: this.form.purpose,
        base: this.form.base,
        content: this.form.content,
        plan: this.form.plan,
        target: this.form.target,
        targets: this.form.targets,
        conditionFirst: this.form.conditionFirst,
        conditionSecond: this.form.conditionSecond,
      };
      // 人员其余参数
      for (let i = 0; i < this.form.user.length; i++) {
        this.form.user[i].personAge = this.form.user[i].age;
        this.form.user[i].personUnit = this.form.user[i].unit;
        this.form.user[i].personWork = this.form.user[i].work;
        this.form.user[i].personZhiji = this.form.user[i].dept;
      }

      // 经费其余参数
      for (let j = 0; j < this.form.funds.length; j++) {
        this.form.funds[j].fundsAmount = this.form.funds[j].amount;
        this.form.funds[j].fundsEnumKey = this.form.funds[j].fundsEnumKey;
      }
      let data = {
        // content: JSON.stringify(content),
        projectName: this.form.projectName,
        projectType: this.form.projectType,
        projectUnit: this.form.projectUnit,
        specialistInfo:
          this.form.specialistInfo1 + "," + this.form.specialistInfo2,
        contractAddress:
          this.form.contractAddress1 + "," + this.form.contractAddress2,
        contractPhone:
          this.form.contractPhone1 + "," + this.form.contractPhone2,
        startTime: this.form.startTime,
        endTime: this.form.endTime,
        cooperateUnit: this.form.cooperateUnit,
        projectCategory: this.pType,
        // 人员
        xmProjectPersonList: this.form.user,
        // 外部人员
        xmProjectOtherPersonList: this.form.otherPerson,
        // 经费
        xmProjectFundsList: this.form.funds,
      };
      console.log(data);
      if (i == 0) {
        this.resetForm();
      } else if (i == 1) {
        data.content = JSON.stringify(content);
        let isEdit = this.$store.state.project.projectInfo.isEdit;

        // isEdit--true,已经暂存过了
        if (isEdit) {
          data.projectId = this.projectId;
          apiUpdateTempSave(data)
            .then((res) => {
              if (res.code == 0) {
                this.msgSuccess("暂存成功");
                this.resetForm();
              } else {
                this.msgError("暂存失败");
              }
            })
            .catch((err) => {
              console.log(err);
              this.msgError("暂存失败");
            });
        } else {
          apitempSave(data)
            .then((res) => {
              if (res.code == 0) {
                this.msgSuccess("暂存成功");
                this.resetForm();
              } else {
                this.msgError("暂存失败");
              }
            })
            .catch((err) => {
              console.log(err);
              this.msgError("暂存失败");
            });
        }
      } else if (i == 2) {
        data.content = JSON.stringify(content);
        let isEdit = this.$store.state.project.projectInfo.isEdit;
        if (isEdit) {
          data.projectId = this.projectId;
          apiUpdateSubmitProject(data)
            .then((res) => {
              if (res.code == 0) {
                this.msgSuccess("发布成功");
                this.resetForm();
              } else {
                this.msgError("发布失败");
              }
            })
            .catch((err) => {
              console.log(err);
              this.msgError("发布失败");
            });
        } else {
          apiSubmitProject(data)
            .then((res) => {
              if (res.code == 0) {
                this.msgSuccess("发布成功");
                this.resetForm();
              } else {
                this.msgError("发布失败");
              }
            })
            .catch((err) => {
              console.log(err);
              this.msgError("发布失败");
            });
        }
      } else if (i == 3) {
        // 渲染用户名称
        for (let i = 0; i < this.peopleList.length; i++) {
          for (let j = 0; j < this.form.user.length; j++) {
            if (this.peopleList[i].uid == this.form.user[j].userId) {
              this.form.user[j].name = this.peopleList[i].name;
            }
          }
        }

        data.purpose = this.form.purpose;
        data.base = this.form.base;
        data.content = this.form.content;
        data.plan = this.form.plan;
        data.target = this.form.target;
        data.targets = this.form.targets;
        data.conditionFirst = this.form.conditionFirst;
        data.conditionSecond = this.form.conditionSecond;

        // 经费预算
        // data.xmProjectFundsList = this.form.funds
        // .map((v) => {
        //   return {
        //     amount: v.amount,
        //     index: v.index,
        //     name: v.name,
        //     note: v.note,
        //   };
        // });

        apiExportProjectWord(data)
          .then((res) => {
            if (res.code === 0) {
              this.downloadFile("" + res.data.url);
            }
          })
          .catch();
      }
    },

    // 删除添加的信息
    delKaoHe(idx) {
      this.form.targets.splice(idx, 1);
      for (let i = 0; i < this.form.targets.length; i++) {
        this.form.targets[i].index = i;
        this.form.targets[i].sort = i;
      }
      this.$forceUpdate();
    },

    // 添加考核信息
    addKaoHe(idx) {
      let row = this.form.targets[idx];
      if (!row.unitName || !row.name || !row.yusuan || !row.zichou) {
        this.msgError("请将信息完善后添加");
        return;
      }
      let myObject = {
        index: idx + 1,
        sort: idx + 1,
        unitName: "",
        name: "",
        yusuan: 0,
        zichou: 0,
      };
      this.form.targets.splice(idx + 1, 0, myObject);
      for (let i = 0; i < this.form.targets.length; i++) {
        this.form.targets[i].index = i;
        this.form.targets[i].sort = i;
      }
      this.$forceUpdate();
    },

    // 添加、删除实施人员
    addPeopleLine(idx, type) {
      if (type == 0) {
        let row = this.form.user[idx];
        if (!row.userId || !row.age || !row.unit || !row.dept || !row.work) {
          this.msgError("请将信息完善后添加");
          return;
        }

        let myObject = {
          index: "",
          userId: "",
          age: 0,
          dept: "",
          unit: "",
          work: "",
        };
        this.form.user.splice(idx + 1, 0, myObject);
        for (let i = 0; i < this.form.user.length; i++) {
          this.form.user[i].index = i;
          this.form.user[i].sort = i;
        }
      } else {
        let row = this.form.otherPerson[idx];
        if (!row.name || !row.age || !row.unit || !row.dept || !row.work) {
          this.msgError("请将信息完善后添加");
          return;
        }

        let myObject = {
          name: "",
          age: 0,
          dept: "",
          unit: "",
          work: "",
          personAge: 0,
          personUnit: "",
          personWork: "",
          personZhiji: "",
        };
        this.form.otherPerson.splice(idx + 1, 0, myObject);
        for (let i = 0; i < this.form.otherPerson.length; i++) {
          this.form.otherPerson[i].sort = i;
        }
      }

      this.$forceUpdate();
    },
    delPeopleLine(idx) {
      this.form.user.splice(idx, 1);
      for (let i = 0; i < this.form.user.length; i++) {
        this.form.user[i].index = i;
        this.form.user[i].sort = i;
      }
      this.$forceUpdate();
    },

    topClickFn(e) {
      this.comName = e == 1 ? "Approval" : "Supplement";
    },

    handleAddItem() {
      let index = this.tableData.length + 1;

      this.tableData.push({
        index,
        name: "",
        text: "",
        address: "",
        address1: "",
      });
    },

    submitData(subtype, name) {
      this.$refs[name].validate((valid) => {
        if (valid) {
          let url = this.form.appendixUrl.map((v) => {
            return {
              fileName: v.fileName,
              relativeFilePath: v.relativeFilePath,
              size: v.size,
              suffix: v.suffix,
              url: v.url,
            };
          });
          let data = {
            projectCategory: this.pType,
            appendixUrl: JSON.stringify(url),
            cooperateUnit: this.form.cooperateUnit,
            primaryUnit: this.form.primaryUnit,
            projectName: this.form.projectName,
            projectCycle:
              this.getFormatedTimeStr(this.form.projectCycle[0]) +
              "-" +
              this.getFormatedTimeStr(this.form.projectCycle[1]),
          };
          // subtype 1 提交 0 暂存
          if (subtype) {
            apiSubmitProject(data)
              .then((res) => {
                if (res.code === 0) {
                  this.msgSuccess("提交成功");
                } else {
                  this.msgError("提交失败");
                }
                this.form = {
                  projectName: "",
                  primaryUnit: "",
                  cooperateUnit: "",
                  projectCycle: [,],
                  appendixUrl: [],
                };
              })
              .catch((err) => {
                console.log(err);
                this.msgError("提交失败");
              });
          } else {
            apitempSave(data)
              .then((res) => {
                if (res.code === 0) {
                  this.msgSuccess("暂存成功");
                } else {
                  this.msgError("暂存失败");
                }
                this.form = {
                  projectName: "",
                  primaryUnit: "",
                  cooperateUnit: "",
                  projectCycle: [,],
                  appendixUrl: [],
                };
              })
              .catch((err) => {
                console.log(err);
                this.msgError("暂存失败");
              });
          }
        }
      });
    },

    getPeoList() {
      apiPeoList()
        .then((res) => {
          if (res.code == 0) {
            this.peopleList = res.data;
          } else {
            this.peopleList = [];
          }
        })
        .catch((err) => {
          console.log(err);
          this.peopleList = [];
        });
    },
    getUserName(id) {
      const index = this.peopleList.findIndex((person) => person.id === id);
      return index !== -1 ? this.peopleList[index].name : null;
    },
    // 获取格式化时间字符串
    getFormatedTimeStr(stamp, str = "YYYY/MM/DD") {
      return moment(stamp).format(str);
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .el-form-item__content {
  text-align: left;
}
.tips {
  font-size: 14px;
  color: #c0c4cc;
  line-height: 16px;
}

.outside_index {
  width: 100%;
  margin: 0 auto;
  padding: 20px;
  display: flex;
  background: #f4f5fc;
  .left {
    flex: 17;
    margin-right: 15px;
    .top_div {
      width: 100% !important;
      background: #fff;
    }

    .bottom {
      width: 100%;
      background: #ffffff;
      // height: 687px;

      .bottom_info {
        height: 100%;
        width: 100%;
        margin: 0 auto;
        position: relative;
        .setBG {
          padding: 30px 70px;
          width: 100% !important;
          background: #ffffff;
          box-shadow: 0px 2px 6px 0px rgba(0, 0, 0, 0.1);
          margin-bottom: 10px;
        }

        .dialog-footer {
          position: absolute;
          left: 50%;
          transform: translateX(-50%);
          bottom: 10px;
        }
      }
    }
  }
  .right {
    flex: 6;
    width: 390px;
    height: 747px;
    // background: #ffffff;
    box-shadow: 0px 2px 6px 0px rgba(0, 0, 0, 0.1);
    border-radius: 0px 0px 0px 0px;
    opacity: 1;
  }
}
</style>
